#include <stl/set.h>
#include <standard/math.h>
#include "terraincolorcmd.h"
#include "editorscenemgr.h"
#include "terrain/terrainobject.h"
#include "terrain/terrainblock.h"

CTerrainColorCmd::CTerrainColorCmd( map<uint32, color32> *pTerrainCellHeightInfoList ){
    map<uint32, color32>::iterator it = pTerrainCellHeightInfoList->begin();
    map<uint32, color32>::iterator it_end = pTerrainCellHeightInfoList->end();
    for(; it != it_end; it++){
        STerrainColorCMDInfo _sTerrainColorCMDInfo;
        uint32 _nKey = it.first();
        _sTerrainColorCMDInfo.m_nX = HIWORD( _nKey );
        _sTerrainColorCMDInfo.m_nY = LOWORD( _nKey );
        _sTerrainColorCMDInfo.m_cOriginColor = it.second();
        _sTerrainColorCMDInfo.m_cNewColor = 0xffffffff;
        m_vTerrainCellColorInfoList.push_back( _sTerrainColorCMDInfo );
    }
}
CTerrainColorCmd::~CTerrainColorCmd(){

}
void CTerrainColorCmd::Init(){
    CTerrainObject* _pTerrainObject = m_pSceneMgr->GetTerrainObject();
    list< STerrainColorCMDInfo >::iterator it = m_vTerrainCellColorInfoList.begin();
    list< STerrainColorCMDInfo >::iterator it_end = m_vTerrainCellColorInfoList.end();
    for(; it != it_end; it++){
        STerrainColorCMDInfo* _pTerrainColorCMDInfo = &(*it);
        _pTerrainColorCMDInfo->m_cNewColor = _pTerrainObject->GetCellColor( _pTerrainColorCMDInfo->m_nX, _pTerrainColorCMDInfo->m_nY );;
    }
}
void CTerrainColorCmd::Final(){

}
bool32 CTerrainColorCmd::Redo(){
    CTerrainBlock* _vTerrainBlock[4];
    set<CTerrainBlock*> _vTerrainBlockList;
    CTerrainObject* _pTerrainObject = m_pSceneMgr->GetTerrainObject();
    list< STerrainColorCMDInfo >::iterator it = m_vTerrainCellColorInfoList.begin();
    list< STerrainColorCMDInfo >::iterator it_end = m_vTerrainCellColorInfoList.end();
    for(; it != it_end; it++){
        STerrainColorCMDInfo* _pTerrainColorCMDInfo = &(*it);
        _pTerrainObject->SetCellColor( _pTerrainColorCMDInfo->m_nX, _pTerrainColorCMDInfo->m_nY, _pTerrainColorCMDInfo->m_cNewColor );
        int32 _nTerrainBlock = _pTerrainObject->GetCellBlock( _pTerrainColorCMDInfo->m_nX, _pTerrainColorCMDInfo->m_nY, _vTerrainBlock );
        for( int32 i= 0 ; i < _nTerrainBlock; i ++ ){
            _vTerrainBlockList.insert( _vTerrainBlock[i] );
        }
    }
    set< CTerrainBlock* >::iterator it0 = _vTerrainBlockList.begin();
    set< CTerrainBlock* >::iterator it0_end = _vTerrainBlockList.end();
    for (; it0 != it0_end; it0++) {
        CTerrainBlock* _pTerrainBlock = it0.first();
        _pTerrainBlock->UpdateGeometry();
    }
    return True;
}
bool32 CTerrainColorCmd::Undo(){
    CTerrainBlock* _vTerrainBlock[4];
    set<CTerrainBlock*> _vTerrainBlockList;
    CTerrainObject* _pTerrainObject = m_pSceneMgr->GetTerrainObject();
    list< STerrainColorCMDInfo >::iterator it = m_vTerrainCellColorInfoList.begin();
    list< STerrainColorCMDInfo >::iterator it_end = m_vTerrainCellColorInfoList.end();
    for(; it != it_end; it++){
        STerrainColorCMDInfo* _pTerrainColorCMDInfo = &(*it);
        _pTerrainObject->SetCellColor( _pTerrainColorCMDInfo->m_nX, _pTerrainColorCMDInfo->m_nY, _pTerrainColorCMDInfo->m_cOriginColor );
        int32 _nTerrainBlock = _pTerrainObject->GetCellBlock( _pTerrainColorCMDInfo->m_nX, _pTerrainColorCMDInfo->m_nY, _vTerrainBlock );
        for( int32 i = 0; i < _nTerrainBlock; i ++ ){
            _vTerrainBlockList.insert( _vTerrainBlock[i] );
        }
    }
    set< CTerrainBlock* >::iterator it0 = _vTerrainBlockList.begin();
    set< CTerrainBlock* >::iterator it0_end = _vTerrainBlockList.end();
    for (; it0 != it0_end; it0++) {
        CTerrainBlock* _pTerrainBlock = it0.first();
        _pTerrainBlock->UpdateGeometry();
    }
    return True;
}

